import json
userinfo = {'teacher':{'00':'tea','01':'tea','02':'tea'},'student':{'15':'stu','16':'stu'}}
with open('passwd',mode = 'w+',encoding='utf8') as f:
    json.dump(userinfo,f)

            

with open('report.json',mode = 'r+',encoding='utf8') as f:
    rep = {'15':{'1':60,'2':30,'3':90}}
    json.dump(rep,f)


# def check_input(valid_set:set):
#     text = ','.join(map(str,valid_set))
#     prompt = '请输入，范围为' + text +':'
#     ipt_val = None
#     while ipt_val not in valid_set:
#         ipt_val = input(prompt)
#     return ipt_val
class User:
    with open('passwd',encoding='utf8') as f:
        __pw_dict = json.load(f)
    check_limit = 3
    def __init__(self,userid):
        self.userid = userid
    @classmethod        
    def pw_check(cls):
        flag = 0
        err_num = 0
        user_type = None
        while user_type not in ('1','2'):
            user_type = input('请输入用户类型，1：老师，2：学生：')
        pw_dict = cls.__pw_dict['teacher'] if user_type == '1' else cls.__pw_dict['student']
        while err_num < cls.check_limit:
            userid = input('请输入用户名：')
            password = input('请输入密码：')
            if pw_dict.get(userid,None) == password:
                flag = 1
                break
            else:
                err_num += 1
        return userid, user_type if flag == 1 else None
    def qurey_report(self, query_id):
        with open('report.json') as f:
            report_dict = json.load(f)
        user_rep = report_dict.get(query_id,None)
        if user_rep:
            for k,v in user_rep.items():
                if k == '1':
                    course = '语文'
                elif k == '2':
                    course = '数学'
                else:
                    course = '英语'
                print('{}成绩:{}'.format(course,v))
        else:
            print('没有成绩')
        

class Teacher(User):
    def create_report(self):
        with open('report.json', mode = 'r+', encoding='utf8') as f:
            report_dict = json.load(f)
            userid = None
            while userid != 'q':
                userid = input('请输入要录入成绩的用户ID，输入q退出：')
                courseid = None
                while courseid not in {'1','2','3'}:
                    courseid = input('请输入课程号:1，语文；2，数学；3，英语：')
                    score = None
                    while not isinstance(score,int):
                        score = input('请输入合法分数0~100：')
                        if isinstance(score,int):
                            if score > 100 or score < 0:
                                continue
                    score = int(score)
                if report_dict.get(userid,None):
                    report_dict.update({userid:{courseid:score}})
                else:
                    report_dict[userid].update({courseid:score})
                json.dump(report_dict,f)

user_id,user_type = User.pw_check()
if user_type == '1':
    u = Teacher(user_id)
    is_create_report = None
    while is_create_report not in ('是','否'):
        is_create_report = input('是否录入成绩：')
    if is_create_report == '是':
        u.create_report()
    else:
        query_id = None
        while query_id != 'q':
            query_id = input('请输入要查询的用户id，输入q退出')
            if query_id != 'q':
                u.qurey_report(query_id)
else:
    u = User(user_id)
    u.qurey_report(u.userid)
    
